package com.vecspace.database.model;

import org.apache.commons.lang3.ArrayUtils;

import com.vecspace.utils.Convert;

/**
 * 
 * @version: 1.0
 * @author: wanghua
 */

public class DataRow {
	
	private Object[] rowdata;
	private DataTable datatable;
	public DataRow(DataTable owner, int attrColumnCount){
		rowdata = new Object[attrColumnCount];
		this.datatable = owner;
	}
	
	public Object getObject(int i){
		return rowdata[i];
	}
	
	public String getString(int i){
		return rowdata[i] == null?"":rowdata[i].toString();
	}

	public int getInt(int i){
		return Convert.toInt(rowdata[i],0);
	}

	public double getDouble(int i){
		return Convert.toDouble(rowdata[i],0.0);
	}
	
	public void setObject(int i, Object value){
		rowdata[i] = value;
	}
	
	public void setObject(String columnName, Object value){
		int idx = datatable.findColumn(columnName);
		if(idx >= 0) {
			setObject(idx, value);
		}
	}
	
	public Object getObject(String columnName){
		int idx = datatable.findColumn(columnName);
		if(idx<0) {
			return null;
		}
		return rowdata[idx];
	}
	
	public void setData(Object[] data){
		if(data == null) {
			return;
		}
		int len = data.length;
		for(int i=0; i<len; ++i){
			this.rowdata[i] = data[i];
		}
	}

	public String getString(String columnName){
		return Convert.toString(getObject(columnName),"");
	}

	public int getInt(String columnName){
		return Convert.toInt(getObject(columnName),0);
	}

	public double getDouble(String columnName){
		return Convert.toDouble(getObject(columnName),0.0);
	}
	
	public Object[] getNativeData(){
		return this.rowdata;
	}
	
	/**在末尾增加一列*/
	protected void appendColumn() {
		rowdata = ArrayUtils.add(rowdata, null);
	}
	/**删除一列*/
	protected void removeColumn(int columnIndex) {
		rowdata = ArrayUtils.remove(rowdata, columnIndex);
	}
}
